﻿using System;

namespace RexToy.UI.Controls
{
    public abstract class ChoiceButton : Control, IChoiceControl
    {
        public abstract bool Checked
        {
            get;
            set;
        }

        protected bool __enable = true;
        public bool Enable
        {
            get
            {
                return __enable;
            }
            set
            {
                if (value)
                {
                    this.__domNode.RemoveClass("ui-disabled");
                }
                else
                {
                    this.__domNode.AppendClass("ui-disabled");
                }

                this.__enable = value;
            }
        }

        private object __value = 0;
        public object Value
        {
            get { return __value; }
        }

        public string[] events = new string[] { "onchange" };

        public ChoiceButton(string text, object value)
        {
            this.__value = value;
            this.__orientation = RexToy.UI.Controls.Orientation.Horizental;
        }

        private void SetButtonClass(ButtonStatus status)
        {
            if (this.__enable)
            {
                var dom = this.__domNode.SearchByName("lbl");
                RexToy.UI.Controls.ButtonStyleHelper.SetDomNodeClass(dom, status, Palette.C);
            }
        }

        protected void OnMouseOver()
        {
            this.SetButtonClass(ButtonStatus.Hover);
        }

        protected void OnMouseDown()
        {
            this.SetButtonClass(ButtonStatus.Down);
        }

        protected void OnMouseUp()
        {
            this.SetButtonClass(ButtonStatus.Up);
        }

        public override void SetFirst(bool first)
        {
            var spn = this.__domNode.SearchByName("spn");
            var lbl = this.__domNode.SearchByName("lbl");
            if (first)
            {
                spn.AppendClass("ui-corner-left");
                lbl.AppendClass("ui-corner-left");
            }
            else
            {
                spn.RemoveClass("ui-corner-left");
                lbl.RemoveClass("ui-corner-left");
            }
        }

        public override void SetLast(bool last)
        {
            var spn = this.__domNode.SearchByName("spn");
            var lbl = this.__domNode.SearchByName("lbl");
            if (last)
            {
                spn.AppendClass("ui-corner-right");
                lbl.AppendClass("ui-corner-right");
            }
            else
            {
                spn.RemoveClass("ui-corner-right");
                lbl.RemoveClass("ui-corner-right");
            }
        }
    }
}
